package com.http;

import java.util.ArrayList;

import org.json.JSONArray;

import android.os.Handler;
import android.util.Log;

import com.android.ocean.util.net.NetConUtil;
import com.android.ocean.util.net.ResEntity;
import com.common.method.ServerPath;
import com.modle.User;
import com.util.JsonUtil1;


public class UserInfoController extends BaseController {
	
	private static UserInfoController singleton;
	//private List<AdvertBean> AdvertBeanList;

	private UserInfoController() {
	}

	public static UserInfoController getInstance() {
		if (singleton == null) {
			singleton = new UserInfoController();
		}
		return singleton;
	}
	
	public void getUserInfo(final Handler handler,final String uid){
	
			new Thread(new Runnable() {
				
				@Override
				public void run() {
					ResEntity re;
					try {
						re = NetConUtil.sendGetRequestEntity(ServerPath.FRIENDS_LIST_PATH+uid);
						if(re.code==ResEntity.S_OK){
							JSONArray jsonArray = new JSONArray(re.jsonStr).getJSONObject(0).getJSONArray("Friends");
							ArrayList<User> list = (ArrayList<User>) JsonUtil1.parseArray(jsonArray.toString(),User.class);
							String content = new JSONArray(re.jsonStr).getJSONObject(0).toString();
							Log.i("test", "解析的内容="+content);
							User user = JsonUtil1.getObject(content, User.class);
							list.add(user);
							sendMsg(handler, 100,99,list);
						
					}else{
						 
					}
					} catch (Exception e) {
					
						e.printStackTrace();
					}
					
					
				}
			}).start();
			
		

	}
	
	
	/**
	 * @param handler
	 * @param uid  用户id
	 * @param phone  电话号码
	 */
	public void register(final Handler handler,final String uid,final String phone){
		
		new Thread(new Runnable() {
			
			@Override
			public void run() {
				ResEntity re;
				try {
					re = NetConUtil.sendGetRequestEntity(ServerPath.REGISTER_PATH+uid+"-"+phone);
					Log.i("test", "请求的路径=="+ServerPath.REGISTER_PATH+uid+"-"+phone);
					if(re.code==ResEntity.S_OK){
						Log.i("test", "同步成功");
//						JSONArray jsonArray = new JSONArray(re.jsonStr).getJSONObject(0).getJSONArray("Friends");
//						ArrayList<User> list = (ArrayList<User>) JsonUtil1.parseArray(jsonArray.toString(),User.class);
//						String content = new JSONArray(re.jsonStr).getJSONObject(0).toString();
//						Log.i("test", "解析的内容="+content);
//						User user = JsonUtil1.getObject(content, User.class);
//						list.add(user);
					sendMsg(handler, 999,1,"sucess");
					
				}else{
					 
				}
				} catch (Exception e) {
					sendMsg(handler, 999,1,"fail");
					Log.i("test", "同步失败");
					e.printStackTrace();
				}
				
				
			}
		}).start();
		
	

}
}
